package com.leaderment.timatt.webmagic.proxyprovider;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.fastjson.JSONObject;
import com.leaderment.timatt.CrawlerExecutorApplication;
import com.leaderment.timatt.SpringContextUtil;
import com.leaderment.timatt.mybatis.bo.CrawlerDomainProxyRedisMappingBO;
import com.leaderment.timatt.mybatis.mapper.CrawlerDomainProxyRedisMappingMapper;
import com.leaderment.timatt.webmagic.extern.MyProxyProvider;
import com.leaderment.timatt.webmagic.extern.ProxyVO;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Task;

/** 
* 代理提供类
* @author kerain
* @date 2018年2月24日
*/
public class CrawlerProxyProvider implements MyProxyProvider {
	RedisTemplate redisTemplate;
	static HashMap<String, String> redisListDomainMap=new HashMap<>();
	Logger logger=Logger.getLogger(CrawlerProxyProvider.class);

	public CrawlerProxyProvider(){
		
	}
	public CrawlerProxyProvider(RedisTemplate redisTemplate){
		this.redisTemplate=redisTemplate;
		this.from();
	}
	
	//初始化代理
	public void from() {
		// 初始化map
		CrawlerDomainProxyRedisMappingMapper crawlerDomainProxyRedisMappingMapper=(CrawlerDomainProxyRedisMappingMapper)SpringContextUtil.getBean("crawlerDomainProxyRedisMappingMapper");
		List<CrawlerDomainProxyRedisMappingBO> list=crawlerDomainProxyRedisMappingMapper.selectDomainAndListName();
		for(int i=0;i<list.size();i++){
			redisListDomainMap.put(list.get(i).getCrawlerRedisListName(), list.get(i).getCrawlerDomain());
		}
    }

	//返回代理
    @Override
    public void returnProxy(ProxyVO proxy, Page page, Task task) {
    	//判断代理是否需要放回
    	Boolean isReturn=proxy.getIsReturn();
    	String proxySource=proxy.getProxySource();
    	if(isReturn==true){
    		redisTemplate.opsForList().rightPush(proxySource, JSONObject.toJSONString(proxy));
    	}
    }

    /**
     * 获取代理
     */
    @Override
    public ProxyVO getProxy(Task task) {
    	ProxyVO p=null;
    	// 根据域名判断使用国内还是国外代理IP
    	String domain=task.getSite().getDomain();
    	String listName=getRedisListByDomain(domain);
    	logger.info("listName:"+listName);
    	//test
    	//Object obj2=redisTemplate.opsForList().leftPop("AboardProxyIP");
    	Object obj2=redisTemplate.opsForList().leftPop(listName);
    	String result=(String) obj2;
    	logger.info("result:"+result);
		//如果redis的List中没有数据，则尝试返回
		if(result==null||("").equals(result)){
			//result=(String) redisTemplate.opsForList().leftPop("MoguProxyIP");
			result=(String) redisTemplate.opsForList().leftPop("MoguProxyIP");
		}
		JSONObject obj=JSONObject.parseObject(result);
		String host=obj.getString("host");
		Integer port=obj.getInteger("port");
		String proxySource=obj.getString("proxySource");
		Boolean isReturn=obj.getBoolean("isReturn");
		String username=obj.getString("username");
		String password=obj.getString("password");
		String expireTime=obj.getString("expireTime");
		p=new ProxyVO(host, port, proxySource, isReturn, username, password, expireTime);
    	return p;
    }
    
    /**
     * 根据域名获取redisList
     * @param domain	域名
     * @return
     */
    public String getRedisListByDomain(String domain){
    	String listName="";
    	for(Entry<String, String> entry : redisListDomainMap.entrySet()) {
    		if(entry.getKey().equals(domain)){
    			listName=entry.getValue();
    		}
    	}
    	return listName;
    }

}
